<?php
include "header.php";
include "mysqli_connection.php";

if (!isset($_SESSION["username"])){
	echo "<script>window.location.href='login.php'</script>";
}
else{
?>
<script>change_picture("admin");</script>
<div>
<h1>Manage Courses</h1>
<a href = "managehome.php">Back to Manage Home</a>
</div>
<div>
<form action = "#" method = "GET">
<label for = "prefix">Prefix: </label><input type = "text" name = "prefix" value = "<?php echo $_GET['prefix'];?>" />
<label for = "number">Number: </label><input type = "text" name = "number" value = "<?php echo $_GET['number'];?>" />
<label for = "title">Title: </label><input type = "text" name = "title" value = "<?php echo $_GET['title'];?>" />
<input type = "submit" value = "Search" />
<input type = "button" value = "Clear" onclick = "window.location.href = 'managecourses.php';" />
</form>
</div>
<div>
<a href="editcourse.php">Add a new course</a>
</div>
<hr />
<div>
<table>
<thead>
<td>ID</td><td>Prefix</td><td>Number</td><td>Title</td><td>Professor</td><td>Operation</td>
</thead>
<tbody>
<?php
$filter = "true";
if (isset($_GET["index"]) && $_GET["index"] !== ""){
	$param = "(c_id = " . $mysqli->real_escape_string(trim($_GET["index"])) . ")";
	$filter .= " AND " . $param;
}
if (isset($_GET["prefix"]) && $_GET["prefix"] !== ""){
	$param = "(prefix = '" . $mysqli->real_escape_string(trim($_GET["prefix"])) . "')";
	$filter .= " AND " . $param;
}
if (isset($_GET["title"]) && $_GET["title"] !== ""){
	$param = "(title LIKE '%" . $mysqli->real_escape_string(trim($_GET["title"])) . "%')";
	$filter .= " AND " . $param;
}
if (isset($_GET["number"]) && $_GET["number"] !== ""){
	$param = "(number = " . $mysqli->real_escape_string(trim($_GET["number"])) . ")";
	$filter .= " AND " . $param;
}
$perNumber = 25;
$page = $_GET['page'];
$count = $mysqli->query("select count(*) from Courses WHERE " . $filter . ";");
$rs = mysqli_fetch_array($count);
$totalNumber = $rs[0];
if ($totalNumber == 0){
	echo "<div><font color='red'>No result!</font></div>";
}
else{
	echo "<div>Found " . $totalNumber. " courses.</div>";
}
$totalPage = ceil($totalNumber / $perNumber);
if ($page < 1) {
	$page = 1;
}
else if ($page > $totalPage){
	$page = $totalPage;
}
$startCount = ($page - 1) * $perNumber;
$result = $mysqli->query("select * from Courses WHERE (" . $filter .") LIMIT $startCount, $perNumber");
if ($result){
	while ($row = mysqli_fetch_array($result)){
?>
<tr>
<td><?php echo $row["c_id"];?></td>
<td><?php echo $row["prefix"];?></td>
<td><?php echo $row["number"];?></td>
<td><?php echo $row["title"];?></td>
<td><?php echo $row["professor"];?></td>
<td><a href="javascript:" onclick = "show_delete_yes(this,<?php echo $row['c_id'];?>);" >Delete</a>&nbsp&nbsp<a href="editcourse.php?id=<?php echo $row['c_id'];?>">Edit</a></td>
</tr>
<?php
	}
}
?>
</tbody>
</table>
<div>
<?php
if ($totalPage > 1){
	if ($page != 1) {
		echo "<a href='managecourses.php?prefix=" . $_GET["prefix"] . "&number=" . $_GET["number"] . "&title=" . $_GET["title"] . "&page=" . ($page - 1) . "'>Back</a>&nbsp";
	}
	for ( $i = 1; $i <= $totalPage; $i++){
		if ($i == $page){
			echo "&nbsp" . $i . "&nbsp";
		}
		else{
			echo "&nbsp<a href='managecourses.php?prefix=" . $_GET["prefix"] . "&number=" . $_GET["number"] . "&title=" . $_GET["title"] . "&page=" . $i . "'>" . $i . "</a>&nbsp";
		}
	}
	if ($page < $totalPage) {
		echo "&nbsp<a href='managecourses.php?prefix=" . $_GET["prefix"] . "&number=" . $_GET["number"] . "&title=" . $_GET["title"] . "&page=" . ($page + 1) . "'>Next</a>";
	}
}
?>
</div>
</div>
<script>
function show_delete_yes(link, id){
	var top = $(link).offset().top;
	$("#deleteyes").css("display","block");
	$("#deleteyes").css("top", top);
	$("#c_id").val(id);
}
function hide_delete_yes(){
	$("#deleteyes").css("display","none");
}
</script>
<div id = "deleteyes" style="display:none;">
<div class = "confirm_delete_text">Do you really want to delete it?</div>
<form action = "deletecourse.php" method = "POST">
<input type = "hidden" name = "id" id = "c_id" />
<input type = "submit" value = "Yes" />
<input type = "button" value = "No" onclick = "hide_delete_yes();false;" />
</form>
</div>
<?php
}
include "footer.php";